var orderId="";

//相关业务逻辑
$(function(){
   page.bind();
   page.getPurchaseOrderData();
});

var table;
var page={//页面总变量
		bind:function(){
			
		},
		getPurchaseOrderData:function(){
			ajaxUtil.ajaxRuquest("approval/getNotApprovalPurchaseOrderData",{},function(data){
				var html="";
				//订单采购
				if(data==null || data.length<=0){ 
					layAlert("没有需要审批的数据");
					return;
				}
				for(var i=0;i<data.length;i++){
					var status="未审核";
					var type="订单采购";
					if(data[i].status==1){
						status="已审核";
					}
					if(data[i].status==2){
						status="退回";
					}
					if(data[i].type==2){
						type="库存采购";
					}
					html+="<div class='top'><span>采购单号:"+data[i].id+"</span> 供应商：<span class='provider'>"+data[i].providerName+"</span>  &nbsp;&nbsp;采购员："+data[i].purchasePeople+
					" &nbsp; &nbsp;币种 ："+data[i].currency+" &nbsp;<span>类型："+type+"</span> &nbsp; &nbsp; <span>状态："+status+"</span> &nbsp; &nbsp;" +
							"<button value='"+data[i].id+"' class='btn btn-primary radius agree'>审核</button>  <button class='notAgree btn btn-primary radius' value='"+data[i].id+"'>退回</button></div>"+tableHtml;
					var detailList=data[i].detailList;
					if(data[i].type==1){ 
						for(var j=0;j<detailList.length;j++){
							var metail=detailList[j].companyMaterialTableBean;
							var feedList=detailList[j].feedDetailList;
							var length=feedList.length+1;
							var sumPurchaseCount=feedList[0].purchaseCount;
							var sumPrice=detailList[j].price*feedList[0].purchaseCount;//总价
							var sumTaxPrice=sumPrice*detailList[j].tax/100;//总含税价
							html+="<tr>"+ 
							"<td rowspan="+length+">"+detailList[j].materialCode+"</td>"+
							"<td rowspan="+length+">"+(metail!=null?metail.materialName:"")+"</td>"+
							"<td rowspan="+length+">"+detailList[j].purchaseCount+"</td>"+
							"<td rowspan="+length+">"+detailList[j].inCount+"</td>"+
							"<td rowspan="+length+">"+(metail!=null?metail.model:"")+"</td>"+
							"<td rowspan="+length+">"+(metail!=null?metail.unit:"")+"</td>"+
							"<td rowspan="+length+">"+detailList[j].price+"</td>"+
							"<td class = 'tax' rowspan="+length+">"+detailList[j].tax+"%</td>"+
							"<td rowspan="+length+">"+(metail!=null?metail.defaultWarehouse:"")+"</td>"+
							"<td rowspan="+length+">"+(metail!=null?metail.materialType:"")+"</td>"+
							"<td>"+feedList[0].orderId+"</td>"+
							"<td>"+sumPurchaseCount+"</td>"+
							"<td>"+sumPrice+"</td>"+
							"<td>"+sumTaxPrice.toFixed(2)+"</td>"+
							"</tr>";
							for(var z=1;z<feedList.length;z++){
								var price=detailList[j].price*feedList[z].purchaseCount;
								sumPurchaseCount+=feedList[z].purchaseCount;
								sumPrice+=price;
								sumTaxPrice+=price*detailList[j].tax/100;
								html+="<tr>"+
								"<td>"+feedList[z].orderId+"</td>"+
								"<td>"+feedList[z].purchaseCount+"</td>"+
								"<td>"+price+"</td>"+
								"<td>"+(price*detailList[j].tax/100).toFixed(2)+"</td>"+
								"</tr>";
							}
							html+="<tr>"+
							"<td>合计</td>"+
							"<td>"+sumPurchaseCount+"</td>"+
							"<td class='sumPrice'>"+sumPrice+"</td>"+
							"<td>"+sumTaxPrice.toFixed(2)+"</td>"+
							"</tr>";
						} 
					}else{
						for(var j=0;j<detailList.length;j++){
							var metail=detailList[j].companyMaterialTableBean;
							var sumPrice=detailList[j].price*detailList[j].purchaseCount;//总价
							var sumTaxPrice=sumPrice*detailList[j].tax/100;//总含税价
							html+="<tr>"+
							"<td>"+detailList[j].materialCode+"</td>"+
							"<td>"+(metail!=null?metail.materialName:"")+"</td>"+
							"<td>"+detailList[j].purchaseCount+"</td>"+
							"<td>"+detailList[j].inCount+"</td>"+
							"<td>"+(metail!=null?metail.model:"")+"</td>"+
							"<td>"+(metail!=null?metail.unit:"")+"</td>"+
							"<td>"+detailList[j].price+"</td>"+
							"<td>"+detailList[j].tax+"%</td>"+
							"<td>"+(metail!=null?metail.defaultWarehouse:"")+"</td>"+
							"<td>"+(metail!=null?metail.materialType:"")+"</td>"+
							"<td>无</td>"+
							"<td>"+detailList[j].purchaseCount+"</td>"+
							"<td>"+sumPrice+"</td>"+
							"<td>"+sumTaxPrice.toFixed(2)+"</td>"+
							"</tr>";
						}
					}
					html+=tableEndHtml;
			} 
			$("#purchaseDiv").html(html);
			page.afterBind();
			});
		},
		afterBind:function(){
			$(".agree").unbind("click");
			$(".notAgree").unbind("click");
			$(".agree").bind("click",function(){
				var id=$(this).val();
				ajaxUtil.ajaxRuquest("approval/setPurchaseStatus",{"id":id,"status":1},function(data){
					layer.msg(data,{icon:1});
					 page.getPurchaseOrderData();
				});
			});
			
			$(".notAgree").bind("click",function(){
				var id=$(this).val();
                ajaxUtil.ajaxRuquest("approval/setPurchaseStatus",{"id":id,"status":2},function(data){
                	 layer.msg(data,{icon:1});
                	 page.getPurchaseOrderData();
				});
			});
		}
}

var tableHtml="<div class='center'>"+
		             "<table class='table table-border table-bordered table-bg table-hover'>"+
		                  "<thead>"+
		                     " <tr>"+
		                     "<th>物料代码</th>"+
	                         "<th>物料名称</th>"+
	                         "<th>采购总数</th>"+
	                         "<th>入库数量</th>"+
	                         "<th>规格型号</th>"+
	                         "<th>单位</th>"+
	                         "<th>含税单价</th>"+
	                         "<th>税率</th>"+
	                         "<th>仓库</th>"+
	                         "<th>物料类型</th>"+
	                         "<th>关联单号</th>"+
	                         "<th>采购数量</th>"+
	                         "<th>含税金额</th>"+
	                         "<th>税额</th>"+
		                      "</tr>"+
		                 "</thead>"+
		                 "<tbody>";
var tableEndHtml="</tbody></table><div>";
